package com.tianzeculture.stagebao.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.DatePicker;
import android.widget.TextView;

import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.network.util.DensityUtil;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by Administrator on 2018/6/5 0005.
 * 时间
 */

public class DateDialog implements View.OnClickListener {

    private Dialog dialog;
    private OnSelectClickListener onSelectClickListener;

    TextView closePop, selectValue, showValue;
    DatePicker datePicker;
    boolean hasDay = false;//true年月日 false年月
    long beginDay = 0;
    long endDay = 0;

    public DateDialog(Context context, String title) {
        init(context, title);
    }

    public DateDialog(Context context, String title, boolean hasDay) {
        this.hasDay = hasDay;
        init(context, title);
    }

    public DateDialog(Context context, String title, boolean hasDay, long beginDay, long endDay) {
        this.hasDay = hasDay;
        this.beginDay = beginDay;
        this.endDay = endDay;
        init(context, title);
    }

    private void init(final Context context, String title) {
        dialog = new Dialog(context, R.style.DialogPushUpInAnimStyle);
        dialog.setContentView(R.layout.popwindow_date);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);

        Window window = dialog.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();

        layoutParams.width = DensityUtil.getScreenWidth(context);
        layoutParams.gravity = Gravity.BOTTOM;
        window.setAttributes(layoutParams);

        closePop = window.findViewById(R.id.closePop);
        closePop.setOnClickListener(this);
        selectValue = window.findViewById(R.id.selectValue);
        selectValue.setText(title);

        showValue = window.findViewById(R.id.showValue);
        showValue.setOnClickListener(this);

        datePicker = window.findViewById(R.id.datePicker);


        if (beginDay != 0) {
            datePicker.setMinDate(beginDay);
        }else{
            datePicker.setMinDate(System.currentTimeMillis());
        }
        if (endDay != 0) {
            datePicker.setMaxDate(endDay);
        }
        if (!hasDay)
            ((ViewGroup) ((ViewGroup) datePicker.getChildAt(0)).getChildAt(0))
                    .getChildAt(2).setVisibility(View.GONE);
        //如果想隐藏掉年，将getChildAt(2)改为getChildAt(0)
    }


    /**
     *
     */
    public void show() {
        dialog.show();
    }

    /**
     *
     */
    public void dismiss() {
        dialog.dismiss();
        if (onSelectClickListener != null) {
            onSelectClickListener.onCancel();
        }
    }

    public OnSelectClickListener getOnSelectClickListener() {
        return onSelectClickListener;
    }

    public void setOnSelectClickListener(OnSelectClickListener onSelectClickListener) {
        this.onSelectClickListener = onSelectClickListener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.closePop:
                dismiss();
                break;

            case R.id.showValue:
                onSelectClickListener.onOk(datePicker.getYear(), datePicker.getMonth() + 1, datePicker.getDayOfMonth());
                dismiss();
                break;
        }
    }


    public interface OnSelectClickListener {
        void onOk(int year, int month, int day);

        void onCancel();
    }


}
